import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:get_storage/get_storage.dart';
import 'package:learning/IconFontIcons.dart';
import 'package:learning/bean/question_entity.dart';
import 'package:learning/components/DataLoadingAndEmpty.dart';
import 'package:learning/components/MyText.dart';
import 'package:learning/generated/l10n.dart';
import 'package:learning/service/QuestionService.dart';
import 'package:timer_count_down/timer_controller.dart';
import 'package:timer_count_down/timer_count_down.dart';

import '../../components/CustomBar.dart';

void redirectTo(
    BuildContext context, String routeName, Map<String, dynamic> arguments) {
  Navigator.pushReplacementNamed(context, routeName, arguments: arguments);
}

class MainExamPage extends StatefulWidget {
  final Object? arguments;

  /// create BrnBubbleText
  const MainExamPage({super.key, this.arguments});

  @override
  // ignore: library_private_types_in_public_api
  _MainExamPageState createState() => _MainExamPageState();
}

class _MainExamPageState extends State<MainExamPage> {
  // Controller
  final CountdownController _controller_timer =
      new CountdownController(autoStart: true);
  final ScrollController _control = new ScrollController();
  final SwiperController _swperController = SwiperController();
  late int total = 100;
  late int subject;
  late int type = 0;
  late String title;
  late int crurentIndex = 0;
  late Future<QuestionEntity> futureData;

  late double integral = 0;
  late int count = 0;
  late int failed = 0;
  late List<int> faileds = [];
  late List<int> qualifieds = [];
  late int qualified = 0;
  late bool success = false;
  late String time = "00:00";
  late String name = "";
  late Map<String, dynamic> parameters = <String, dynamic>{};

  late Map<int, String> selectedItems = <int, String>{};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    parameters = widget.arguments as Map<String, dynamic>;
    subject = parameters["subject"] as int;
    type = parameters["type"] as int;
    title = parameters["title"];
    print("Param:$subject");
    // ModalRoute.of(context)!.settings.arguments;
    if (type != 0) {
      futureData = fetchTodoLearn();
    } else {
      futureData = fetchTodo();
    }
  }

  Future<QuestionEntity> fetchTodo() async {
    final box = GetStorage();
/*    box.write("categoryId", item.id);
    box.write("categoryName", item.name);
    box.write("categoryContent", item.content);*/

    int categoryId = box.hasData("categoryName") ? box.read("categoryId") : 0;
    //listData=[];
    int langId = S.current.locale == "zh_CN" ? 1 : 2;
    QuestionEntity listData =
        await QuestionService.getExamList(langId, subject, categoryId);
    if (listData.total > 0) {
      return listData;
    } else {
      throw Exception('Failed to load data');
    }
  }

  Future<QuestionEntity> fetchTodoLearn() async {
    final box = GetStorage();
/*    box.write("categoryId", item.id);
    box.write("categoryName", item.name);
    box.write("categoryContent", item.content);*/

    int categoryId = box.hasData("categoryName") ? box.read("categoryId") : 0;
    //listData=[];
    int langId = S.current.locale == "zh_CN" ? 1 : 2;
    QuestionEntity listData = await QuestionService.getExamLearnList(
        langId, subject, categoryId, type);
    if (listData.total > 0) {
      return listData;
    } else {
      throw Exception('Failed to load data');
    }
  }

  @override
  Widget build(BuildContext context) {
    // Controller
/*    final CountdownController _controller =
        new CountdownController(autoStart: true);*/
    print("LLLLLLLLLLLLLLLLLLLLLLLLLL:$subject");
    /*  List<ExamItem> list = [];
    for (int i = 0; i < 10; i++) {
      var a = ExamItem();
      a.content = "بۇ بولسا  سىناق مەزمۇن بۇنى سىناشقىلا  ئىشلىتىمىز";
      list.add(a);
    }*/
//Flying Cars  App logo   driver，Education
    // TODO: implement build
    return Scaffold(
      appBar: CustomBar(
        title: this.title,
      ),
      body: FutureBuilder<QuestionEntity>(
          future: futureData,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              QuestionEntity questionEntity = snapshot.requireData;
              List<QuestionList> list = questionEntity.list;
              return Column(
                children: [
                  Container(
                      padding: const EdgeInsets.all(10),
                      decoration: const BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 0.7,
                                  color: Colors.black12,
                                  style: BorderStyle.solid))),
                      height: 40,
                      width: MediaQuery.of(context).size.width,
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Countdown(
                              controller: _controller_timer,
                              seconds: 50 * 60,
                              build: (_, double timeS) {
                                String t = getTimeminuteView(timeS);

                                return Text(
                                  t,
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Colors.red,
                                  ),
                                );
                              },
                              interval: Duration(milliseconds: 100),
                              onFinished: () {
                                print('Timer is done!');
                              },
                            ),
                            Text("${questionEntity.total}/${crurentIndex + 1}")
                          ])),
                  Expanded(
                      flex: 1,
                      child: Container(
                          color: Colors.white,
                          width: MediaQuery.of(context).size.width,
                          child: Swiper(
                            index: crurentIndex,
                            controller: _swperController,
                            onIndexChanged: (index) {
                              if (index <= crurentIndex + 1) {
                                setState(() {
                                  crurentIndex = index;
                                });
                              } else {
                                /*     setState(() {
                                  crurentIndex = 0;
                                });*/
                                //   _swperController.move(0);
                              }
                            },
                            //   next:(){},
                            itemBuilder: (context, index) {
                              QuestionList item = list[index];
                              return ExamContent(
                                index: index,
                                item: item,
                                selected: selectedItems.containsKey(item.id),
                                selectedKey: selectedItems[item.id],
                                type: type,
                                callbackValid: (bool b, String selectedKey) {
                                  selectedItems.putIfAbsent(
                                      item.id, () => selectedKey);
                                  if (b) {
                                    _swperController.next();
                                    success = true;
                                    count++;
                                    qualified++;
                                    qualifieds.add(item.id);
                                  } else {
                                    success = false;
                                    count++;
                                    failed++;
                                    faileds.add(item.id);
                                  }
                                  print("Bool:$b");
                                },
                              );
                            },
                            itemCount: list.length,
                            //  pagination: const SwiperPagination(),
                            //  control: const SwiperControl(),
                          ))),
                  ansawerButtomNavigation(list),
                ],
              );
            } else {
              return snapshot.hasError ? DataEmpty() : DataLoading();
            }
          }),
    );
  }

  ansawerButtomNavigation(List<QuestionList> list) {
    List<Widget> listWidget = [
      Expanded(
          flex: 1,
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text("${this.qualified}",
                    style: TextStyle(color: Colors.blueAccent)),
                Icon(
                  IconFontIcons.iconAHuaban2fuben52,
                  color: Colors.blueAccent,
                )
              ],
            ),
          )),
      Expanded(
          flex: 1,
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text("${this.failed}",
                    style: TextStyle(color: Colors.redAccent)),
                Icon(
                  IconFontIcons.iconAHuaban2fuben53,
                  color: Colors.redAccent,
                )
              ],
            ),
          )),
      Expanded(
          flex: 1,
          child: InkWell(
              onTap: () {
                _showBottomSheet(context, list);
              },
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text("${this.crurentIndex + 1}/${this.total}",
                        style: TextStyle(color: Colors.black54)),
                    Icon(
                      IconFontIcons.iconCategory,
                      color: Colors.black54,
                    )
                  ],
                ),
              )))
    ];
    if (this.type == 0) {
      listWidget.add(Expanded(
          flex: 1,
          child: Center(
            child: InkWell(
                onTap: () {
                  _controller_timer.onResume!();
                  Map<String, dynamic> data = new Map();
                  data.putIfAbsent("faileds", () => this.faileds);
                  data.putIfAbsent("qualifieds", () => this.qualifieds);
                  data.putIfAbsent("count", () => this.count);
                  data.putIfAbsent("success", () => true);
                  data.putIfAbsent("subject", () => this.subject);
                  data.putIfAbsent("integral", () => this.integral);
                  data.putIfAbsent("failed", () => this.failed);
                  data.putIfAbsent("qualified", () => this.qualified);
                  data.putIfAbsent("time", () => this.time);

                  QuestionService.sendData(data).then((value) {
                    BrnDialogManager.showConfirmDialog(context,
                        //  showIcon: true,
                        title: S.of(context).kaoshitijiaoquedingma,
                        titleWidget: Container(
                          padding: EdgeInsets.only(top: 20, bottom: 10),
                          child: MyText(S.of(context).qinxuanzecheleixing),
                        ),
                        confirm: S.of(context).quedeng,
                        cancel: S.of(context).quxiao,
                        conformWidget: InkWell(
                            onTap: () {
                              /*  Navigator.pushNamed(
                                        context, "/mainexamresult",
                                        arguments: data);*/
                              redirectTo(context, "/mainexamresult", data);
                              BrnToast.show("确定", context);
                            },
                            child:
                                Center(child: MyText(S.of(context).quedeng))),
                        cancelWidget: InkWell(
                            onTap: () {
                              BrnToast.show("取消", context);
                              Navigator.pop(context);
                            },
                            child:
                                Center(child: MyText(S.of(context).quxiao))));
                  });
                },
                child: Container(
                  padding: EdgeInsets.all(5),
                  margin: EdgeInsets.all(5),
                  width: MediaQuery.of(context).size.width - 30,
                  decoration: BoxDecoration(
                      color: Colors.blue.shade100,
                      borderRadius: BorderRadius.circular(10.0)),
                  child: Icon(
                    IconFontIcons.iconSend,
                    color: Colors.blue.shade700,
                  ),
                )),
          )));
    }

    return Container(
        decoration: const BoxDecoration(
            //  color: Colors.black12,
            border: Border(
                top: BorderSide(
                    width: 0.5,
                    color: Colors.black12,
                    style: BorderStyle.solid))),
        height: 50,
        width: MediaQuery.of(context).size.width,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: listWidget,
        ));
  }

  void _showBottomSheet(BuildContext context, List<QuestionList> list) {
    print("_showBottomSheet");
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return Container(
          width: MediaQuery.of(context).size.width,
          padding: EdgeInsets.all(10.0),
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: 300.0, // 设置最小高度为200像素
            ),
            child: Column(
              children: [
                Container(
                  width: MediaQuery.of(context).size.width,
                  padding: EdgeInsets.only(bottom: 5),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              width: 0.5,
                              color: Colors.black12,
                              style: BorderStyle.solid))),
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Center(
                            child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: Icon(IconFontIcons.iconGuanbi1),
                        )),
                        Expanded(
                            flex: 1,
                            child: Center(
                              child: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Text("${this.qualified}",
                                      style:
                                          TextStyle(color: Colors.blueAccent)),
                                  Icon(
                                    IconFontIcons.iconAHuaban2fuben52,
                                    color: Colors.blueAccent,
                                  )
                                ],
                              ),
                            )),
                        Expanded(
                            flex: 1,
                            child: Center(
                              child: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Text("${this.failed}",
                                      style:
                                          TextStyle(color: Colors.redAccent)),
                                  Icon(
                                    IconFontIcons.iconAHuaban2fuben53,
                                    color: Colors.redAccent,
                                  )
                                ],
                              ),
                            )),
                        Expanded(
                            flex: 1,
                            child: InkWell(
                                onTap: () {},
                                child: Center(
                                  child: Column(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Text(
                                          "${this.crurentIndex + 1}/${this.total}",
                                          style:
                                              TextStyle(color: Colors.black54)),
                                      Icon(
                                        IconFontIcons.iconCategory,
                                        color: Colors.black54,
                                      )
                                    ],
                                  ),
                                ))),
                      ],
                    ),
                  ),
                ),
                Expanded(flex: 1, child: _numberLine(context, list))
              ],
            ),
          ),
        );
      },
    );
  }

  _numberLine(BuildContext context, List<QuestionList> listData) {
    List<Widget> list = List.generate(100, (index) {
      QuestionList itemData = listData[index];
      return Center(
        child: InkWell(
          onTap: () {
            setState(() {
              crurentIndex = index;
              _swperController.move(index);
            });
          },
          child: Container(
            padding: EdgeInsets.all(5.0),
            height: 40.0,
            width: 40.0,
            decoration: BoxDecoration(
              //   border: Border.all(color: Colors.blueAccent),
              color: activeColor(itemData),
              borderRadius: BorderRadius.all(Radius.circular(100.0)),
            ),
            child: Center(
              child: Text('${(index + 1)}',
                  style: TextStyle(
                      color: activeTextColor(itemData), fontSize: 16.0)),
            ),
          ),
        ),
      );
    });
    return GridView(
        padding: EdgeInsets.all(10.0),
        primary: false,
        shrinkWrap: true,
        controller: _control,
        scrollDirection: Axis.vertical,
        physics: BouncingScrollPhysics(),
        // 回弹效果
        cacheExtent: 200.0,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5,
          // 假设我们有20等分的网格
          crossAxisSpacing: 5.0,
          // 横向间距
          mainAxisSpacing: 5.0,
          // 纵向间距
          childAspectRatio: 3 / 2,
          // 子Widget宽高比为1:1
          mainAxisExtent: 50.0, // 主轴（纵向）上的跨度，每个子Widget占据5个单位
        ),
        children: list);
  }

  activeColor(QuestionList itemData) {
    if (this.selectedItems.containsKey(itemData.id)) {
      String? key = this.selectedItems[itemData.id];
      if (key == itemData.answer) {
        return Colors.blue.shade100;
      } else {
        return Colors.redAccent.shade100;
      }
    } else {
      return Colors.black.withAlpha(12);
    }
  }

  activeTextColor(QuestionList itemData) {
    if (this.selectedItems.containsKey(itemData.id)) {
      String? key = this.selectedItems[itemData.id];
      if (key == itemData.answer) {
        return Colors.blue.shade700;
      } else {
        return Colors.redAccent.shade700;
      }
    } else {
      return Colors.black;
    }
  }

  String getTimeminuteView(double timeS) {
    if (timeS < 0) {
      return "00:00";
    } else {
      int minute = timeS.toInt() ~/ 60;
      int second = timeS.toInt() % 60;
      if (minute == 0) {
        return "00:${second.toString().padLeft(2, '0')}";
      }
      String vTime =
          "${minute.toString().padLeft(2, '0')}:${second.toString().padLeft(2, '0')}";
      /*   setState(() {
        this.time = vTime;
      });*/
      time = vTime;
      return vTime;
    }
  }
}

class ExamContent extends StatefulWidget {
  @override
  // ignore: library_private_types_in_public_api
  _ExamContentState createState() => _ExamContentState();
  final int index;
  final int type;
  final QuestionList item;
  final GestureExamCallback<bool>? callbackValid;
  final bool selected;
  final String? selectedKey;

  const ExamContent(
      {super.key,
      required this.index,
      required this.item,
      required this.selected,
      this.selectedKey = "",
      this.type = 0,
      this.callbackValid});
}

class _ExamContentState extends State<ExamContent> {
  late int index;
  late int type;
  late QuestionList item;
  late String? selected = "";
  late String? selectedKey = "";
  late String? answerKey = "";
  late bool selectedValid = false;
  late GestureExamCallback<bool>? callbackValid;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    index = widget.index;
    type = widget.type;
    item = widget.item;
    answerKey = widget.item.answer;
    callbackValid = widget.callbackValid;
    if (widget.selected) {
      selected = widget.selectedKey;
      selectedKey = widget.selectedKey;

      selectedValid = true;
      print("HHHHHHHHHHHHHHHHHHHHHHHHHHH:${widget.selectedKey}");
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> itemWdits = [
      MyText(
        item.content,
        softWrap: true,
        style: TextStyle(color: Colors.black),
      )
    ];
    if (item.type == 1) {
      itemWdits.add(ansawerXuanze(context, item, callbackValid));
    }
    if (item.type == 2) {
      itemWdits.add(ansawerPanduan(context, item, callbackValid));
    }
    if (item.type == 3) {
      itemWdits.add(ansawerPanduan(context, item, callbackValid));
    }
    if (item.type != 0) {
      itemWdits.add(getValidtInfo());
    }
    return Container(
        alignment: AlignmentDirectional.topStart,
        padding: EdgeInsets.all(10),
        height: MediaQuery.of(context).size.height - 100,
        width: MediaQuery.of(context).size.width,
        child: ListView(
          children: itemWdits,
        ));
  }

  Widget ansawerPanduan(BuildContext context, QuestionList item,
      GestureExamCallback<bool>? callbackValid) {
    bool isSuccess = false;

    return Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.only(top: 10, bottom: 10),
      alignment: AlignmentDirectional.topStart,
      child: Column(mainAxisAlignment: MainAxisAlignment.start, children: [
        ansawerItem(context, "A", item.answer, item.optionA, selected == "A",
            callbackValid),
        ansawerItem(context, "B", item.answer, item.optionB, selected == "B",
            callbackValid),
      ]),
    );
  }

  Widget ansawerXuanze(BuildContext context, QuestionList item,
      GestureExamCallback<bool>? callbackValid) {
    bool isSuccess = false;

    return Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.only(top: 10, bottom: 10),
      alignment: AlignmentDirectional.topStart,
      child: Column(mainAxisAlignment: MainAxisAlignment.start, children: [
        ansawerItem(context, "A", item.answer, item.optionA, selected == "A",
            callbackValid),
        ansawerItem(context, "B", item.answer, item.optionB, selected == "B",
            callbackValid),
        ansawerItem(context, "C", item.answer, item.optionC, selected == "C",
            callbackValid),
        ansawerItem(context, "D", item.answer, item.optionD, selected == "D",
            callbackValid),
      ]),
    );
  }

  Widget ansawerItem(BuildContext context, String keyStr, String sucKey,
      String content, bool success, GestureExamCallback<bool>? callbackValid) {
    Color colorLayout = Colors.black.withAlpha(12);
    Color colorText = Colors.black;
    Color colorLayerText = Colors.white;
    Color colorLayerFullText = Colors.black;

    if (selectedValid) {
      if (sucKey == keyStr) {
        if (this.type != 0) {
          colorLayout = Colors.greenAccent.shade100;
          colorLayerText = Colors.greenAccent.shade700;
          colorLayerFullText = Colors.greenAccent.shade700;
          colorText = Colors.white;
        }
      } else {
        if (keyStr == selected) {
          if (this.type == 0) {
            colorLayout = Colors.blue.shade100;
            colorLayerText = Colors.blue.shade700;
            colorLayerFullText = Colors.blue.shade700;
            colorText = Colors.white;
          } else {
            colorLayout = Colors.redAccent.shade100;
            colorLayerText = Colors.redAccent.shade700;
            colorLayerFullText = Colors.redAccent.shade700;
            colorText = Colors.white;
          }
        } else {
          if (sucKey == selected) {
            // colorLayout = Colors.blue.shade100;
            if (this.type == 0) {
              colorLayout = Colors.blue.shade100;
              colorLayerText = Colors.blue.shade700;
              colorLayerFullText = Colors.blue.shade700;
              colorText = Colors.white;
            }
          } else {
            /*   if(this.type==0) {
              colorLayout = Colors.blue.shade100;
              colorLayerText = Colors.blue.shade700;
              colorLayerFullText = Colors.blue.shade700;
              colorText = Colors.white;
            }*/
          }
        }
      }
    }
    return InkWell(
        onTap: () {
          setState(() {
            if (this.selectedValid == false) {
              this.selectedValid = true;
              //  item.selectedOption=keyStr;
              selected = keyStr;
              print("Key: $keyStr   suckey:$sucKey");
              if (sucKey.contains(keyStr)) {
                if (callbackValid != null) {
                  callbackValid(true, keyStr);
                }
              } else {
                if (callbackValid != null) {
                  callbackValid(false, keyStr);
                }
              }
            }
          });
        },
        child: Container(
          width: MediaQuery.of(context).size.width,
          padding: EdgeInsets.only(top: 10, bottom: 10, left: 5, right: 5),
          margin: EdgeInsets.only(top: 5, bottom: 5),
          alignment: AlignmentDirectional.topStart,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(6),
            color: colorLayout,
            //  boxShadow: [BoxShadow(color: Colors.grey, blurRadius: 0.1)],
          ),
          child: Row(mainAxisAlignment: MainAxisAlignment.start, children: [
            Container(
              width: 30,
              height: 30,
              margin: EdgeInsets.only(left: 5, right: 5),
              decoration: BoxDecoration(
                color: colorLayerText,
                shape: BoxShape.circle,
                boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 6)],
              ),
              child: Center(
                  child: Text(
                keyStr,
                style: TextStyle(color: colorText),
              )),
            ),
            Expanded(
                flex: 1,
                child: MyText(content.trim(),
                    softWrap: true,
                    overflow: TextOverflow.fade,
                    style: TextStyle(color: colorLayerFullText)))
          ]),
        ));
  }

  getValidtInfo() {
    if (selectedValid) {
      return Container(
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6),
          color: Colors.grey.shade100,
          //  boxShadow: [BoxShadow(color: Colors.grey, blurRadius: 0.1)],
        ),
        padding: EdgeInsets.all(10),
        alignment: AlignmentDirectional.topStart,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Row(children: [
              MyText("${S.current.zhengquedangan}:${item.answer}",
                  style: TextStyle(fontSize: 14)),
              SizedBox(width: 20),
              MyText("${S.current.xuanzedangan}:${selected}",
                  style: TextStyle(fontSize: 14)),
            ]),
            SizedBox(height: 12),
            Row(children: [
              MyText("${S.current.shuoming}",
                  style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: Colors.blue)),
              Text(":")
            ]),
            MyText(
                (item.remark != null && item.remark != 'null')
                    ? item.remark
                    : '----',
                softWrap: true,
                style: TextStyle(fontSize: 14))
          ],
        ),
      );
    } else {
      return SizedBox(height: 30);
    }
  }
}

typedef GestureExamCallback<bool> = void Function(
    bool isSuccess, String selectedKey);
